Page({
  data: {
    departments: [
      { id: 1, name: '内科' },
      { id: 2, name: '外科' },
      { id: 3, name: '儿科' },
      { id: 4, name: '妇产科' }
    ],
    selectedDeptId: 1,
    allDoctors: [
      // 内科
      { id: 1, deptId: 1, name: '张三', title: '主任医师', specialty: '心脑血管疾病', avatar: '/images/doctor/doctor1.jpg' },
      { id: 2, deptId: 1, name: '李四', title: '副主任医师', specialty: '消化内科', avatar: '/images/doctor/doctor2.jpg' },
      // 外科
      { id: 3, deptId: 2, name: '王五', title: '主治医师', specialty: '创伤骨科', avatar: '/images/doctor/doctor3.jpg' },
      // 儿科
      { id: 4, deptId: 3, name: '赵六', title: '主治医师', specialty: '儿童保健', avatar: '/images/doctor/doctor4.jpg' },
      // 妇产科
      { id: 5, deptId: 4, name: '孙七', title: '主任医师', specialty: '妇产科疾病', avatar: '/images/doctor/doctor5.jpg' }
    ],
    doctors: [],
    consultType: 'text',
    medicalRecords: [],
    messages: [],
    inputMessage: '',
    isLoading: false
  },

  onLoad() {
    this.filterDoctorsByDept();
  },

  filterDoctorsByDept() {
    const filtered = this.data.allDoctors.filter(doctor => doctor.deptId === this.data.selectedDeptId);
    this.setData({ doctors: filtered });
  },

  selectDepartment(e) {
    const deptId = e.currentTarget.dataset.id;
    this.setData({ selectedDeptId: deptId }, () => this.filterDoctorsByDept());
  },

  chooseConsultType(e) {
    this.setData({ consultType: e.detail.value });
  },

  uploadMedicalRecord() {
    wx.chooseMessageFile({
      count: 3,
      type: 'file',
      success: (res) => {
        const files = res.tempFiles.map(file => ({ name: file.name }));
        this.setData({ medicalRecords: [...this.data.medicalRecords, ...files] });
      }
    });
  },

  onMessageInput(e) {
    this.setData({ inputMessage: e.detail.value });
  },

  sendMessage() {
    const msg = this.data.inputMessage.trim();
    if (!msg) return;

    const newMsg = { content: msg, sender: 'user', time: Date.now() };
    this.setData({
      messages: [...this.data.messages, newMsg],
      inputMessage: '',
      isLoading: true
    });

    // 模拟医生回复
    setTimeout(() => {
      const doctorMsg = { content: '收到消息，正在处理...', sender: 'doctor', time: Date.now() };
      this.setData({
        messages: [...this.data.messages, doctorMsg],
        isLoading: false
      });
    }, 2000);
  }
});    